{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.cluster import DBSCAN\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "from sklearn.preprocessing import PowerTransformer\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('B22.csv')\n",
    "# 删除id列(对聚类无用)\n",
    "df = df.drop(['文物采样点','文物编号','纹饰','颜色','Unnamed: 0','Unnamed: 21'], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 玻璃类型为高钾玻璃\n",
    "df = df.loc[df['类型']== 1]\n",
    "df = df.drop(['类型','表面风化'],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据标准化\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "scaler = StandardScaler()\n",
    "data_scaled = scaler.fit_transform(df)\n",
    "df_scaled =  pd.DataFrame(data_scaled, columns=df.columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "col = df.columns\n",
    "dbscan = DBSCAN(eps=2,min_samples=6)\n",
    "mm = MinMaxScaler()\n",
    "#归一化\n",
    "df_dbscan = mm.fit_transform(df)\n",
    "pt = PowerTransformer()\n",
    "df_dbscan = pt.fit_transform(df_dbscan)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'df_dbscan' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32mc:\\Users\\32531\\Desktop\\CUMCM2022\\聚类\\DBSCAN.ipynb Cell 5\u001b[0m in \u001b[0;36m<cell line: 9>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      <a href='vscode-notebook-cell:/c%3A/Users/32531/Desktop/CUMCM2022/%E8%81%9A%E7%B1%BB/DBSCAN.ipynb#W4sZmlsZQ%3D%3D?line=6'>7</a>\u001b[0m     \u001b[39mreturn\u001b[39;00m np\u001b[39m.\u001b[39marray(k_dist)\n\u001b[0;32m      <a href='vscode-notebook-cell:/c%3A/Users/32531/Desktop/CUMCM2022/%E8%81%9A%E7%B1%BB/DBSCAN.ipynb#W4sZmlsZQ%3D%3D?line=7'>8</a>\u001b[0m k \u001b[39m=\u001b[39m \u001b[39m4\u001b[39m  \n\u001b[1;32m----> <a href='vscode-notebook-cell:/c%3A/Users/32531/Desktop/CUMCM2022/%E8%81%9A%E7%B1%BB/DBSCAN.ipynb#W4sZmlsZQ%3D%3D?line=8'>9</a>\u001b[0m k_dist \u001b[39m=\u001b[39m select_MinPts(df_dbscan,k)\n\u001b[0;32m     <a href='vscode-notebook-cell:/c%3A/Users/32531/Desktop/CUMCM2022/%E8%81%9A%E7%B1%BB/DBSCAN.ipynb#W4sZmlsZQ%3D%3D?line=9'>10</a>\u001b[0m k_dist\u001b[39m.\u001b[39msort()\n\u001b[0;32m     <a href='vscode-notebook-cell:/c%3A/Users/32531/Desktop/CUMCM2022/%E8%81%9A%E7%B1%BB/DBSCAN.ipynb#W4sZmlsZQ%3D%3D?line=10'>11</a>\u001b[0m \u001b[39mprint\u001b[39m(k_dist)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'df_dbscan' is not defined"
     ]
    }
   ],
   "source": [
    "def select_MinPts(data,k):\n",
    "    k_dist = []\n",
    "    for i in range(data.shape[0]):\n",
    "        dist = (((data[i] - data)**2).sum(axis=1)**0.5)\n",
    "        dist.sort()\n",
    "        k_dist.append(dist[k])\n",
    "    return np.array(k_dist)\n",
    "k = 4  \n",
    "k_dist = select_MinPts(df_dbscan,k)\n",
    "k_dist.sort()\n",
    "print(k_dist)\n",
    "plt.plot(np.arange(k_dist.shape[0]),k_dist[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "eps:\n",
      "4.492607234644307\n"
     ]
    }
   ],
   "source": [
    "eps = k_dist[::-1][10]\n",
    "print(\"eps:\")\n",
    "print(eps)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAW8AAAD4CAYAAAAjKGdbAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8qNh9FAAAACXBIWXMAAAsTAAALEwEAmpwYAAAgj0lEQVR4nO3deXxV9Z3/8dfnZiULIXuAAJEtEVGWxiq4lE2raLXWVmmtbaft0NbakY7+5tHF7rX9zW/qaBdrpbbTjtqWDmpdinVQ0LrXoEH2RfZAQiAsIYGs398f54IBEnIDOTn33ryfj8d53Ms95968PVzfnHzPZs45REQktoSCDiAiIj2n8hYRiUEqbxGRGKTyFhGJQSpvEZEYlOjHh+bl5bmSkhI/PlpEJC4tW7Zsj3MuP9LlfSnvkpISKioq/PhoEZG4ZGZbe7K8hk1ERGKQyltEJAapvEVEYpDKW0QkBqm8RURikMpbRCQGqbxFRGJQ1JR3W7vjvqUbWb59f9BRRESiXtSU96GmVh55fSvzFlTS0NQadBwRkagWNeWdNSCJ/7xxIlv2NvC9p1YFHUdEJKpFVN5mNsjMFprZWjNbY2ZT/Ahz4chcbpk2ij9X7OCv7+zy40eIiMSFSLe8fwr8zTlXBkwA1vgVaN6ssUwYNoivP/YOVfsP+/VjRERiWrflbWZZwKXAbwCcc83Ouf1+BUpKCPGzORNpa3d8dUElb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R8j+4HRgETgV14wxCx4uOcequ7x+s4qPKO5KbGx5Yxs0QgC9jbJ+k6YWZJeMX9iHPusRPnO+cOOucOhZ8vApLMLK+PY56YqSr8uBt4HO9Xyo6i9ebSVwJvOedqTpwRjes5rObokFP4cXcny0Td+jazzwBXAzeF/9E5SQTfoz7hnKtxzrU559qBX3eRIxrXcSLwEWBBV8uczjoOqrwjuanxk8DRvfEfBZZ09eXyW3i86jfAGufcf3axTNHRMXkzez/eug3yH5t0M8s8+hxv59TKExZ7EvhU+KiTC4EDHX71D1KXWynRtp476Ph9/TTwRCfLPAtcbmbZ4V/5Lw+/FggzuwL4N+Aa51xjF8tE8j3qEyfsj7muixzReMP0WcBa59yOzmae9jrui72wXexdnY131Ma7wDfDr30f74sEkIr3a/NG4B/AyACzXoz3a/A7QGV4mg18EfhieJlbgVV4e7dfB6YGlTecZ2Q4y/JwrqPruGNmA+4L/x2sAMqDzBzOlI5XxlkdXouq9Yz3D8suoAVvTPVzePtjngc2AM8BOeFly4EHO7z3s+Hv9EbgnwLOvBFvfPjod/ro0V1DgEWn+h4FlPeh8Pf0HbxCHnxi3vCfT+qWoDKHX//d0e9vh2XPeB3r9HgRkRikHZYiIjFI5S0iEoNU3iIiMUjlLSISg1TeIiIxSOUtIhKDVN4iIjHo/wOADiFKaUW4aAAAAABJRU5ErkJggg==",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(np.arange(k_dist.shape[0]),k_dist[::-1])\n",
    "plt.scatter(10,eps,color=\"r\")\n",
    "plt.plot([0,10],[eps,eps],linestyle=\"--\",color = \"r\")\n",
    "plt.plot([10,10],[0,eps],linestyle=\"--\",color = \"r\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'eps' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32mc:\\Users\\32531\\Desktop\\CUMCM2022\\聚类\\DBSCAN.ipynb Cell 8\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> <a href='vscode-notebook-cell:/c%3A/Users/32531/Desktop/CUMCM2022/%E8%81%9A%E7%B1%BB/DBSCAN.ipynb#X10sZmlsZQ%3D%3D?line=0'>1</a>\u001b[0m dbscan \u001b[39m=\u001b[39m DBSCAN(eps\u001b[39m=\u001b[39meps,min_samples\u001b[39m=\u001b[39m\u001b[39m4\u001b[39m)\n\u001b[0;32m      <a href='vscode-notebook-cell:/c%3A/Users/32531/Desktop/CUMCM2022/%E8%81%9A%E7%B1%BB/DBSCAN.ipynb#X10sZmlsZQ%3D%3D?line=1'>2</a>\u001b[0m dbscan\u001b[39m.\u001b[39mfit(df_scaled)\n\u001b[0;32m      <a href='vscode-notebook-cell:/c%3A/Users/32531/Desktop/CUMCM2022/%E8%81%9A%E7%B1%BB/DBSCAN.ipynb#X10sZmlsZQ%3D%3D?line=2'>3</a>\u001b[0m \u001b[39m#分类结果\u001b[39;00m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'eps' is not defined"
     ]
    }
   ],
   "source": [
    "dbscan = DBSCAN(eps=eps,min_samples=4)\n",
    "dbscan.fit(df_scaled)\n",
    "#分类结果\n",
    "y_dbscan_pred = dbscan.labels_\n",
    "print(max(y_dbscan_pred))\n",
    "print(min(y_dbscan_pred))\n",
    "#共分为6类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 各个类的个数\n",
    "df_dbscan_clusters = pd.concat([df_scaled, pd.DataFrame({\"ClusterID\":dbscan.labels_})], axis=1)\n",
    "df_dbscan_clusters.ClusterID.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 评估聚类的效果\n",
    "from sklearn import metrics\n",
    "metrics.calinski_harabasz_score(df_scaled, y_dbscan_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import silhouette_score\n",
    "silhouette_score(df_scaled, y_dbscan_pred)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "3b93323d9187cfbd1820f773ea0df511273ea71bb4ff41615b7e3b3c08803218"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
